<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="[]"
            :quick-search-placeholder="t('quick Search Placeholder', { fields: t('xBjbody.quick Search Fields') })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" >
            <template #columnPrepend>
                <el-table-column  type="expand">
                    <template #default="table">
                        <el-table :data="table.row.body" :border="false">
                            <el-table-column width="80"/>
                            <el-table-column label="费用项目" prop="ITEMCODE" />
                            <el-table-column label="单价" prop="UNITPRICE" />
                            <el-table-column label="币别" prop="MONEYTYPE" />
                            <el-table-column label="净价" prop="NOTAXPRICE" />
                            <el-table-column label="税率" prop="TAXRATE" />
                            <!-- <el-table-column label="计价单位" prop="COSTTYPE" /> -->
                            <!-- <el-table-column label="天数" prop="TRANSDAYS" /> -->
                            <!-- <el-table-column label="提罐地" prop="LOADPLACE" /> -->
                            <!-- <el-table-column label="装罐地" prop="DISPLACE" /> -->
                            <!-- <el-table-column label="返回地" prop="RETURNPLACE" /> -->
                            <!-- <el-table-column label="供应商" prop="VENDOR" /> -->
                            <el-table-column label="备注" prop="NOTE" />
                            <el-table-column label="操作" v-if="props.formOperate!=='view'&&!isApprove">
                                <template #default="scope">
                                    <el-button class=".table-operate" type="primary"  size="small" style="padding:4px 5px" @click="handleEditItem(scope.row)">
                                        <span><i class="fa fa-pencil icon" style="font-size: 14px; color: rgb(255, 255, 255);"></i></span>
                                    </el-button>
                                    <el-button class=".table-operate" type="danger" size="small" style="padding:4px 5px" @click="handleDelItem(scope.row)">
                                        <span><i class="fa fa-trash icon" style="font-size: 14px; color: rgb(255, 255, 255);"></i></span>
                                    </el-button>
                                </template>
                            </el-table-column>


                        </el-table>
                    </template>
                </el-table-column>
            </template>
            <template  #columnAppend v-if="props.formOperate!=='view'&&!isApprove"> 
                <el-table-column  label="">
                    <template #default="props">
                        <el-button type="primary" size="small" @click="addItem(props.row)">{{ t('add') }}</el-button>
                    </template>
                </el-table-column>
            </template>
        </Table>

        <!-- 表单 -->
        <PopupForm v-if="baTable.form.operate" />

        
        <ItemForm v-if="costItem.operate" @onSubmit="handleItemSubmit" />

    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive , inject } from 'vue'
import baTableClass from '/@/utils/baTable'
import { xBjbody } from '/@/api/controllerUrls'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import ItemForm from './itemForm.vue'
import {
  Delete,
} from '@element-plus/icons-vue'

import createAxios from '/@/utils/axios'
import { ElMessage } from 'element-plus'


const { t } = useI18n()
const props = defineProps({
formOperate:String
})
const isApprove = inject('isApprove') as boolean

const tableRef = ref()
const optButtons = defaultOptButtons(["delete"])
const baTable = new baTableClass(
    new baTableApi(xBjbody),
    {
        pk: 'ID',
        column: [
            // { type: 'selection', align: 'center', operator: false },
            // { label: t('xBjbody.ID'), prop: 'ID', align: 'center', operator: 'RANGE' },
            { label: t('xBjbody.BJNO'), prop: 'BJNO', align: 'center' },
            { label: t('xBjbody.LOADPLACE'), prop: 'LOADPLACE', align: 'center' },
            { label: t('xBjbody.DISPLACE'), prop: 'DISPLACE', align: 'center' },
            { label: '运输时间', prop: 'day', align: 'center' },
            { label: '应收价', prop: 'rmb_price', align: 'center' },
            { label: '成本支出', prop: 'rmb_costs', align: 'center' },
            { label: '利润', prop: 'profit', align: 'center' },
            { label: '净价利润', prop: 'net_profit', align: 'center' },
            { label: '利润率', prop: 'margin', align: 'center' },
            // { label: t('operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false ,show:props.formOperate!=='view'},
        ],
        dblClickNotEditColumn: [undefined, ],
        defaultOrder: { prop: 'ID', order: 'desc' },
    },
    {
        defaultItems: [],
    }
)

provide('baTable', baTable)

const bjno = inject('bjno')


onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.table.filter!.gstype = '1'
    baTable.table.filter!.type = 2
    baTable.table.filter!.bjno = bjno

    baTable.mount()
    baTable.getIndex()?.then(() => {
        console.log(baTable.table.data)
        // baTable.initSort()
        // baTable.dragSort()
    })
})


const costItem = reactive({
    operate:'',
    form:{},
    lineData:{}
})

provide('costItem',costItem)

const addItem = (item:any)=>{
    costItem.operate = '新增'
    costItem.lineData = item
    costItem.form = {}
    // console.log(costItem,item)
}

const handleItemSubmit = ()=>{
    baTable.getIndex()
}
const handleEditItem = (item:any)=>{
    costItem.operate = '编辑'
    // costItem.lineData = lineData
    costItem.form = item

}
const handleDelItem = (item:any)=>{
    createAxios({
      url:'/admin/xBjbody/del',
      data:{
        ids:[item.ID]
      },
      method:'delete'
    }).then((res:any)=>{
        ElMessage({
            message: '删除成功',
            type: 'success',
        })
        baTable.getIndex()
    })
}

</script>

<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
    name: 'xBjbody',
})
</script>

<style scoped lang="scss"></style>
